/*
 * @Author: szx
 * @Date: 2022-04-05 13:19:04
 * @LastEditTime: 2022-04-05 13:33:11
 * @Description:
 * @FilePath: \leetcode\剑指offerII\034\034.js
 */
/**
 * @param {string[]} words
 * @param {string} order
 * @return {boolean}
 */
var isAlienSorted = function (words, order) {
    const map = {};
    for (let i = 0; i < order.length; i++) {
        map[order[i]] = i;
    }
    for (let i = 1; i < words.length; i++) {
        const left = words[i - 1];
        const right = words[i];
        const leftLen = left.length;
        const rightLen = right.length;
        let j = 0;
        for (; j < leftLen && j < rightLen; j++) {
            if (map[left[j]] > map[right[j]]) {
                return false;
            } else if (map[left[j]] < map[right[j]]) {
                break;
            }
        }
        if ((j == leftLen || j == rightLen) && leftLen > rightLen) return false;
    }
    return true;
};
console.log(isAlienSorted(['hello', 'leetcode'], 'hlabcdefgijkmnopqrstuvwxyz'));
console.log(isAlienSorted(['word', 'world', 'row'], 'worldabcefghijkmnpqstuvxyz'));
console.log(isAlienSorted(['apple', 'app'], 'abcdefghijklmnopqrstuvwxyz'));
